package com.crm.biz.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.annotation.SystemServiceLog;
import com.crm.model.BizProject;
import com.crm.model.BizProjectLevel;
import com.crm.model.BizScale;
import com.crm.model.SysStudentAttr;
import com.crm.service.BizProjectLevelService;
import com.crm.service.BizProjectService;
import com.crm.service.BizScaleService;
import com.crm.service.SysStudentAttrService;
import com.crm.util.AopConfig;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.UUIDUtil;
import com.github.pagehelper.Page;
import com.lowagie.text.DocumentException;

@Controller
@RequestMapping("/bizScale")
public class BizScaleController {
	
	@Autowired
	private BizScaleService bizScaleService;
	
	@Autowired
	private BizProjectService bizProjectService;
	
	@Autowired
	private BizProjectLevelService bizProjectLevelService;
	
	@Autowired
	private SysStudentAttrService sysStudentAttrService;
	
	@RequestMapping("/index")
	public String index(){
		
		return "configuration/branchDivide";
	}
	
	@RequestMapping("/load")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<BizScale> page, BizScale t){
		
		Page<BizScale> list = (Page<BizScale>) bizScaleService.selectAll(page, t);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@SystemServiceLog(description=AopConfig.SCALE+AopConfig.ADD,type=AopConfig.ADD)
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(BizScale record){
		
		Map<String, Object> map = new HashMap<>();
		record.setScaleId(UUIDUtil.getUUID());
		bizScaleService.insert(record);
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@SystemServiceLog(description=AopConfig.SCALE+AopConfig.UPDATE,type=AopConfig.UPDATE)
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizScale record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizScaleService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	/**
	 * 
	 * @Description : 删除操作
	 * @author zealot
	 * @param @param record
	 * @param @return
	 * @return Map<String,Object>
	 * @version 创建时间 : 2016年11月18日 下午7:25:47
	 * @thorws
	 */
	@SystemServiceLog(description=AopConfig.SCALE+AopConfig.DEL,type=AopConfig.DEL)
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(BizScale record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		bizScaleService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	/**
	 * 
	 * @Description : 项目级别检索
	 * @author zealot
	 * @param @param request
	 * @param @param response
	 * @param @param record
	 * @return void
	 * @version 创建时间 : 2016年11月23日 下午3:55:58
	 * @thorws
	 */
	@RequestMapping("/loadProjectLevel")
	public void loadProjectLevel(HttpServletRequest request,HttpServletResponse response,BizProjectLevel record){
		List<BizProjectLevel> list = bizProjectLevelService.selectByUs(record);
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	/**
	 * 
	 * @Description : 项目检索
	 * @author zealot
	 * @param @param request
	 * @param @param response
	 * @param @param record
	 * @return void
	 * @version 创建时间 : 2016年11月23日 下午3:57:01
	 * @thorws
	 */
	@RequestMapping("/loadProject")
	public void loadProject(HttpServletRequest request,HttpServletResponse response,BizProject record){
		List<BizProject> list = bizProjectService.selectByUs(record);
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	/**
	 * 
	 * @Description : pdf导出
	 * @author zealot
	 * @param @param response
	 * @param @throws IOException
	 * @param @throws SecurityException
	 * @param @throws NoSuchMethodException
	 * @param @throws IllegalArgumentException
	 * @param @throws IllegalAccessException
	 * @param @throws InvocationTargetException
	 * @param @throws InstantiationException
	 * @param @throws DocumentException
	 * @return void
	 * @version 创建时间 : 2016年11月23日 下午7:04:52
	 * @thorws
	 */
	@RequestMapping("/downloadPDF")
	public void downloadPDF(HttpServletResponse response) throws IOException, SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, DocumentException{
		List<BizScale> list = bizScaleService.selectByUs(null);
		ResponseUtil.downloadPublicPDF(list, response, "BizScale",BizScale.class);
	}
	
	/**
	 * 
	 * @Description : 咨询者类型
	 * @author zealot
	 * @param @param t
	 * @param @param response
	 * @return void
	 * @version 创建时间 : 2016年11月25日 下午2:11:22
	 * @thorws
	 */
	@RequestMapping("/loadSysStudentAttr")
	public void loadSysStudentAttr(SysStudentAttr t,HttpServletResponse response){
		List<SysStudentAttr> list = sysStudentAttrService.selectByUs(t);
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
}
